﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Pub;
using System.Text.RegularExpressions;
using System.IO;

namespace JT_Regular
{
    public partial class F_Regular : Form
    {
        string sFile = "";
        MatchCollection matches;
        int i_index = 0;

        public F_Regular()
        {
            InitializeComponent();
        }

        private void F_Regular_Load(object sender, EventArgs e)
        {
            tboxTxt.Dock = DockStyle.Fill;
            tboxResult.Dock = DockStyle.Fill;

            new CRichTextBoxMenu(tboxTxt);
            new CRichTextBoxMenu(tboxResult);
        }

        private void btnTest_Click(object sender, EventArgs e)
        {
            bool b1 = false;
            for (int i = 0; i < tboxRegExp.Items.Count;i++ )
            {
                if (tboxRegExp.Text == tboxRegExp.Items[i].ToString())
                {
                    b1 = true;
                    break;
                }
            }
            if (!b1) tboxRegExp.Items.Add(tboxRegExp.Text);

            tboxResult.Clear();
            Regex reg = new Regex(tboxRegExp.Text);
            matches = reg.Matches(tboxTxt.Text);

            lab1.Text = "find: " + matches.Count;
            lab2.Text = "---";
            i_index = 0;

            if (matches.Count > 0)
            {
                tboxResult.Text = matches[0].Value;
                lab2.Text = "index: " + (i_index + 1);
            }
        }

        private void btnOpen_Click(object sender, EventArgs e)
        {
            OpenFileDialog dlg = new OpenFileDialog();
            dlg.Filter = "测试数据(*.RegExp)|*.RegExp";
            dlg.InitialDirectory = Application.StartupPath + "\\RegExp";

            if (dlg.ShowDialog() == DialogResult.OK)
            {
                sFile = dlg.FileName;

                List<string> lst1 = new List<string>();
                CFile.Read(sFile,ref lst1);

                tboxRegExp.Text = lst1[lst1.Count - 1];
                lst1.RemoveAt(lst1.Count - 1);

                tboxTxt.Clear();
                foreach (string s1 in lst1)
                {
                    tboxTxt.AppendText(s1 + "\n");
                }
            }
        }

        private void btnSave_Click(object sender, EventArgs e)
        {
            if (!string.IsNullOrEmpty(sFile))
            {
                string s1 = tboxTxt.Text;
                s1 += "\r\n" + tboxRegExp.Text;

                CFile.Write(sFile, s1);
            }
            else
            {
                btnSaveAs_Click(btnSaveAs, e);
            }
        }

        private void btnSaveAs_Click(object sender, EventArgs e)
        {
            SaveFileDialog dlg = new SaveFileDialog();
            dlg.Filter = "测试数据(*.RegExp)|*.RegExp";
            dlg.InitialDirectory = Application.StartupPath + "\\RegExp";

            if (dlg.ShowDialog() == DialogResult.OK)
            {
                sFile = dlg.FileName;

                string s1 = tboxTxt.Text;
                s1 += "\r\n" + tboxRegExp.Text;

                CFile.Write(sFile, s1);
            }
        }

        private void btnNext_Click(object sender, EventArgs e)
        {
            if (i_index < matches.Count - 1)
            {
                i_index++;
                lab2.Text = "index: " + (i_index + 1);
                tboxResult.Text = matches[i_index].Value;
            }
        }

        private void btnPre_Click(object sender, EventArgs e)
        {
            if (i_index > 0)
            {
                i_index--;
                lab2.Text = "index: " + (i_index + 1);
                tboxResult.Text = matches[i_index].Value;
            }
        }

        private void btnShowAll_Click(object sender, EventArgs e)
        {
            tboxResult.Clear();
            for (int i = 0; i < matches.Count; i++)
            {
                tboxResult.Text += matches[i].Value + "\n";
            }
        }

        private void btnTestLine_Click(object sender, EventArgs e)
        {
            bool b1 = false;
            for (int i = 0; i < tboxRegExp.Items.Count; i++)
            {
                if (tboxRegExp.Text == tboxRegExp.Items[i].ToString())
                {
                    b1 = true;
                    break;
                }
            }
            if (!b1) tboxRegExp.Items.Add(tboxRegExp.Text);

            tboxResult.Clear();
            Regex reg = new Regex(tboxRegExp.Text);
            Match match = reg.Match(tboxTxt.Text);

            lab1.Text = "find: 0";
            lab2.Text = "---";
            i_index = 0;

            if (match.Success)
            {
                lab1.Text = "find: 1";
                tboxResult.Text = match.Value;
                lab2.Text = "index: 1";
            }
        }
    }

    public static class JTBOX
    {
        /// <summary>
        /// 注册插件
        /// </summary>
        /// <param name="node1"></param>
        /// <param name="list1"></param>
        /// <returns></returns>
        public static bool RegPlug(List<Form> list1, ImageList imgLst)
        {
            try
            {
                F_Regular f1 = new F_Regular();
                list1.Add(f1);

                imgLst.Images[list1.Count - 1] = f1.imageList2.Images[0];
            }
            catch (System.Exception ex)
            {
                CDebug.SendError(ex.ToString());
                return false;
            }
            return true;
        }
    }
}
